package com.dream.cgomall.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dream.cgomall.dao.CgoAdminDao;
import com.dream.cgomall.entity.CgoAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 目前暂不使用Security进行权限与用户验证
 *
 *
 *
 *
 *
 */
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    @Autowired(required = false)
    public CgoAdminDao cgoAdminDao;
    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
        QueryWrapper<CgoAdmin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        CgoAdmin cgoAdmin = cgoAdminDao.selectOne(queryWrapper);
        //判断返回为null，没有这个手机注册
        if(cgoAdmin!=null){
            throw new UsernameNotFoundException("用户名不存在！");
        }

        //根据手机查询
        List<GrantedAuthority> auths= AuthorityUtils.commaSeparatedStringToAuthorityList("roles");
        return new User(cgoAdmin.getPhone(),new BCryptPasswordEncoder().encode(""),auths);
    }
}
